package com.pmsystem.service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.NewsMapper;
import com.pmsystem.pojo.News;
import com.pmsystem.pojo.NewsExample;
import com.pmsystem.pojo.NewsExample.Criteria;

@Service
public class NewsService {

	@Autowired
	NewsMapper newsMapper;
	
	/**
	 * 
	 * <p>Title: writeNews</p>  
	 * <p>Description:编写新闻草稿 </p>  
	 * @author 树昌
	 * @param news
	 * @return
	 */
	public boolean writeNews(News news) {
		int row = newsMapper.insertSelective(news);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: showEditorNews</p>  
	 * <p>Description:编辑者条件查询新闻界面 </p>  
	 * @author 树昌
	 * @param keyword
	 * @param startRow
	 * @param pageSize
	 * @return
	 */
	public List<News> showEditorNews(String keyword, Integer startRow, Integer pageSize){
		
		NewsExample newsExample = new NewsExample();
		Criteria criteria = newsExample.createCriteria();
		criteria.andEnableNotEqualTo((byte) 0);
		
		if (keyword != null) {
			keyword = "%" + keyword + "%";
			criteria.andNewsTitleLike(keyword);
			Criteria criteria2=newsExample.or();
			criteria2.andNewsTitleLike(keyword);
		}
		// 分页查询
		if (startRow != null && pageSize != null) {
			if (((startRow-1)*pageSize) >= 0 && pageSize >= 1) {
				newsExample.setStartRow((startRow-1)*pageSize);
				newsExample.setPageSize(pageSize);
			} else {
				return null;
			}
		}
		// 没有条件则为全查询
		return newsMapper.selectByExample(newsExample);
	}
	
	
	
	/**
	 * 
	 * <p>Title: releaseNews</p>  
	 * <p>Description:发布新闻 </p>  
	 * @author 树昌
	 * @param news
	 */
	public boolean releaseNews(News news) {

		NewsExample newsExample = new NewsExample();
		Criteria criteria = newsExample.createCriteria();
		criteria.andNewsIdEqualTo(news.getNewsId());
		news.setEnable((byte) 2);
		Date time = new Date(System.currentTimeMillis());
		news.setTime(time);

		int row = newsMapper.updateByExampleSelective(news, newsExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: showReaderNews</p>  
	 * <p>Description:阅读者查询新闻 </p>  
	 * @author 树昌
	 * @param keyword
	 * @param startRow
	 * @param pageSize
	 * @return
	 */
	public List<News> showReaderNews(String keyword, Integer startRow, Integer pageSize) {
		NewsExample newsExample = new NewsExample();
		Criteria criteria = newsExample.createCriteria();
		criteria.andEnableEqualTo((byte) 2);
		
		if (keyword != null) {
			keyword = "%" + keyword + "%";
			criteria.andNewsTitleLike(keyword);
			Criteria criteria2=newsExample.or();
			criteria2.andNewsTitleLike(keyword);
		}
		// 分页查询
		if (startRow != null && pageSize != null) {
			if (((startRow-1)*pageSize) >= 0 && pageSize >= 1) {
				newsExample.setStartRow((startRow-1)*pageSize);
				newsExample.setPageSize(pageSize);
			} else {
				return null;
			}
		}
		// 没有条件则为全查询
		List<News> listnews = newsMapper.selectByExample(newsExample);
		for (News news : listnews) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			System.out.println(sdf.format(news.getTime()));
			System.out.println(news.getTime());
			
		}
		return listnews;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteNews</p>  
	 * <p>Description:删除一条新闻 </p>  
	 * @author 树昌
	 * @param news
	 * @return
	 */
	public boolean deleteNews(News news) {
		
		NewsExample newsExample = new NewsExample();
		Criteria criteria = newsExample.createCriteria();
		news.setEnable((byte)0);
		criteria.andNewsIdEqualTo(news.getNewsId());
		int row = newsMapper.updateByExampleSelective(news, newsExample);
		
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: updateNews</p>  
	 * <p>Description:修改新闻内容 </p>  
	 * @author 树昌
	 * @param news
	 * @return
	 */
	public boolean updateNews(News news) {
		
		NewsExample newsExample =new NewsExample();
		Criteria criteria = newsExample.createCriteria();
		criteria.andNewsIdEqualTo(news.getNewsId());
		
		int row = newsMapper.updateByExampleSelective(news, newsExample);
		return row==1?true:false;
	}
}
